<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.1.1">
  <link rel="apple-touch-icon" sizes="180x180" href="/hexo/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/hexo/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/hexo/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/hexo/images/logo.svg" color="#222">

<link rel="stylesheet" href="/hexo/css/main.css">


<link rel="stylesheet" href="/hexo/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"youchen12138.gitee.io","root":"/hexo/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}}};
  </script>

  <meta name="description" content="什么是正则表达式由以下两种字符组成的文字模式  普通字符（例如26个英文字母、数字、汉字、_、,，不含有特殊含义的符号等）  特殊字符（有特殊含义的，例如.\等）   该模式描述在查找文字主体时待匹配的一个或多个字符串。正则表达式作为一个模板，将某个字符模式与说搜索的字符串进行匹配 正则表达式匹配的一定是字符串中的内容 方法test如果找到，则返回 true 如果未找到，则返回 false 123">
<meta property="og:type" content="article">
<meta property="og:title" content="JS正则表达式">
<meta property="og:url" content="https://youchen12138.gitee.io/hexo/2020/11/08/JS%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/index.html">
<meta property="og:site_name" content="幽辰的博客">
<meta property="og:description" content="什么是正则表达式由以下两种字符组成的文字模式  普通字符（例如26个英文字母、数字、汉字、_、,，不含有特殊含义的符号等）  特殊字符（有特殊含义的，例如.\等）   该模式描述在查找文字主体时待匹配的一个或多个字符串。正则表达式作为一个模板，将某个字符模式与说搜索的字符串进行匹配 正则表达式匹配的一定是字符串中的内容 方法test如果找到，则返回 true 如果未找到，则返回 false 123">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2020-11-08T11:00:11.000Z">
<meta property="article:modified_time" content="2020-11-08T11:00:40.999Z">
<meta property="article:author" content="幽辰">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">

<link rel="canonical" href="https://youchen12138.gitee.io/hexo/2020/11/08/JS%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'en'
  };
</script>

  <title>JS正则表达式 | 幽辰的博客</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/hexo/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">幽辰的博客</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/hexo/" rel="section"><i class="fa fa-home fa-fw"></i>Home</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/hexo/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>Tags<span class="badge">23</span></a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/hexo/categories/" rel="section"><i class="fa fa-th fa-fw"></i>Categories<span class="badge">14</span></a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/hexo/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>Archives<span class="badge">127</span></a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="en">
    <link itemprop="mainEntityOfPage" href="https://youchen12138.gitee.io/hexo/2020/11/08/JS%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/hexo/images/avatar.jpg">
      <meta itemprop="name" content="幽辰">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="幽辰的博客">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JS正则表达式
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">Posted on</span>
              

              <time title="Created: 2020-11-08 19:00:11 / Modified: 19:00:40" itemprop="dateCreated datePublished" datetime="2020-11-08T19:00:11+08:00">2020-11-08</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">In</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/hexo/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
                </span>
            </span>

          

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="什么是正则表达式"><a href="#什么是正则表达式" class="headerlink" title="什么是正则表达式"></a>什么是正则表达式</h2><p>由以下两种字符组成的文字模式</p>
<ol>
<li><p>普通字符（例如26个英文字母、数字、汉字、<code>_</code>、<code>,</code>，不含有特殊含义的符号等）</p>
</li>
<li><p>特殊字符（有特殊含义的，例如<code>.\</code>等）</p>
</li>
</ol>
<p>该模式描述在查找文字主体时待匹配的一个或多个字符串。正则表达式作为一个模板，将某个字符模式与说搜索的字符串进行匹配</p>
<p>正则表达式匹配的一定是字符串中的内容</p>
<h2 id="方法"><a href="#方法" class="headerlink" title="方法"></a>方法</h2><h3 id="test"><a href="#test" class="headerlink" title="test"></a>test</h3><p>如果找到，则返回 true</p>
<p>如果未找到，则返回 false</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">var str &#x3D; &quot;I love js&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;js&#x2F;;</span><br><span class="line"></span><br><span class="line">console.log(pattern.test(str));</span><br></pre></td></tr></table></figure>

<h3 id="exec"><a href="#exec" class="headerlink" title="exec"></a>exec</h3><p>如果找到，则将在字符串中找到的该字符传入一个数组中，并将该数组返回</p>
<p>如果未找到，则返回 null</p>
<h2 id="flags"><a href="#flags" class="headerlink" title="flags"></a>flags</h2><table>
<thead>
<tr>
<th>flags</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>g</td>
<td>表示全局(global)模式，即模式将被应用于所有字符串，而非在发现第一个匹配项时立即停止</td>
</tr>
<tr>
<td>i</td>
<td>表示不区分(case-insensitive)模式，即在确定匹配项时忽略模式与字符串的大小写</td>
</tr>
<tr>
<td>m</td>
<td>表示多行模式，即在到达一行文本末尾时还会继续查找下一行中是否存在于模式匹配的项</td>
</tr>
</tbody></table>
<h1 id="转义字符"><a href="#转义字符" class="headerlink" title="转义字符"></a>转义字符</h1><ul>
<li><p>如果使用字面量的方式定义正则，一个反斜杠转义</p>
</li>
<li><p>如果使用构造函数方式定义正则，需要用到双重反斜杠</p>
</li>
</ul>
<p>有问请查看文件中的<code>特殊字符总结</code>，<code>常用正则表达式总结</code></p>
<table>
<thead>
<tr>
<th>常用的字符类</th>
<th>意义</th>
</tr>
</thead>
<tbody><tr>
<td><code>/./</code></td>
<td>表示除了换行符之外的所有字符</td>
</tr>
<tr>
<td><code>/\w/</code></td>
<td>等同于<code>/[a-zA-Z0-9_]/</code></td>
</tr>
<tr>
<td><code>/\W/</code></td>
<td>等同于<code>/[^a-zA-Z0-9_]/</code></td>
</tr>
<tr>
<td><code>/\d/</code></td>
<td>等同于<code>/[0-9]/</code></td>
</tr>
<tr>
<td><code>/\D/</code></td>
<td>等同于<code>/[^0-9]/</code></td>
</tr>
<tr>
<td><code>/\s/</code></td>
<td>匹配空格和制表符之类的</td>
</tr>
<tr>
<td><code>/\S/</code></td>
<td>匹配空格和制表符之类的空白字符以外的所有字符</td>
</tr>
<tr>
<td><code>/\b/</code></td>
<td>单词的边界匹配</td>
</tr>
</tbody></table>
<p>中文的 Unicode 编码为<code>\u4e00-\u9fa5</code></p>
<h1 id="重复"><a href="#重复" class="headerlink" title="重复"></a>重复</h1><p>以下均为贪婪匹配</p>
<p>代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 匹配n个字符 --&gt;</span><br><span class="line">var str &#x3D; &quot;110&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;\d&#123;3&#125;&#x2F;; &#x2F;&#x2F; &#123;&#125;中表示匹配的个数</span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;110&quot;]</span><br><span class="line"></span><br><span class="line">&lt;!-- 匹配至少n个字符 --&gt;</span><br><span class="line">var str &#x3D; &quot;110&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;\d&#123;1,&#125;&#x2F;; &#x2F;&#x2F; 至少1个</span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;110&quot;]</span><br><span class="line"></span><br><span class="line">&lt;!-- 匹配n个到m个 --&gt;</span><br><span class="line">var str &#x3D; &quot;110&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;\d&#123;0,1&#125;&#x2F;; </span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;1&quot;]</span><br><span class="line"></span><br><span class="line">&lt;!-- 匹配0个或者1个 --&gt;</span><br><span class="line">var str &#x3D; &quot;110&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;\d?&#x2F;; </span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;1&quot;]</span><br><span class="line"></span><br><span class="line">&lt;!-- 至少匹配1个 --&gt;</span><br><span class="line">var str &#x3D; &quot;110&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;\d+&#x2F;; </span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;110&quot;]</span><br><span class="line"></span><br><span class="line">&lt;!-- 至少匹配0个 --&gt;</span><br><span class="line">var str &#x3D; &quot;110&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;\d*&#x2F;; </span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;110&quot;]</span><br></pre></td></tr></table></figure>

<h2 id="非贪婪匹配"><a href="#非贪婪匹配" class="headerlink" title="非贪婪匹配"></a>非贪婪匹配</h2><p>在量词后面加<code>?</code></p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 至少匹配1个 --&gt;</span><br><span class="line">var str &#x3D; &quot;110&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;\d+?&#x2F;; </span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;1&quot;]</span><br></pre></td></tr></table></figure>

<h1 id="选择、分组、引用"><a href="#选择、分组、引用" class="headerlink" title="选择、分组、引用"></a>选择、分组、引用</h1><h2 id="选择"><a href="#选择" class="headerlink" title="选择"></a>选择</h2><p>代码示例</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var str &#x3D; &quot;html js&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;html|css|js&#x2F;; </span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;html&quot;]</span><br></pre></td></tr></table></figure>

<h2 id="分组"><a href="#分组" class="headerlink" title="分组"></a>分组</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">var str &#x3D; &quot;ababab&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;(ab)+&#x2F;; </span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;ababab&quot;, &quot;ab&quot;]</span><br><span class="line"></span><br><span class="line">&lt;!-- 非捕获性分组 --&gt;</span><br><span class="line">var str &#x3D; &quot;ababab&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;(?:ab)+&#x2F;; </span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;ababab&quot;]</span><br></pre></td></tr></table></figure>

<h2 id="引用"><a href="#引用" class="headerlink" title="引用"></a>引用</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 捕获性分组的引用 --&gt;</span><br><span class="line">var str &#x3D; &quot;ab cd ab&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;(ab) cd \1&#x2F;; </span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;ab cd ab&quot;, &quot;ab&quot;]</span><br></pre></td></tr></table></figure>

<h1 id="首尾匹配"><a href="#首尾匹配" class="headerlink" title="首尾匹配"></a>首尾匹配</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 首匹配 --&gt;</span><br><span class="line">var str1 &#x3D; &quot;js&quot;;</span><br><span class="line">var str2 &#x3D; &quot;html js&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;^js&#x2F;;</span><br><span class="line">console.log(pattern.exec(str1)); &#x2F;&#x2F; [&quot;js&quot;]</span><br><span class="line">console.log(pattern.exec(str2)); &#x2F;&#x2F; null</span><br><span class="line"></span><br><span class="line">&lt;!-- 尾匹配 --&gt;</span><br><span class="line">var str1 &#x3D; &quot;js&quot;;</span><br><span class="line">var str2 &#x3D; &quot;js html&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;js$&#x2F;;</span><br><span class="line">console.log(pattern.exec(str1)); &#x2F;&#x2F; [&quot;js&quot;]</span><br><span class="line">console.log(pattern.exec(str2)); &#x2F;&#x2F; null</span><br></pre></td></tr></table></figure>

<h1 id="前瞻性匹配"><a href="#前瞻性匹配" class="headerlink" title="前瞻性匹配"></a>前瞻性匹配</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 在这个例子中，只有java后面跟了script才会匹配成功 --&gt;</span><br><span class="line">var str &#x3D; &#39;javascript&#39;;</span><br><span class="line">var pattern &#x3D; &#x2F;java(?&#x3D;script)&#x2F;;</span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; [&quot;java&quot;]</span><br></pre></td></tr></table></figure>

<h1 id="负向前瞻性匹配"><a href="#负向前瞻性匹配" class="headerlink" title="负向前瞻性匹配"></a>负向前瞻性匹配</h1><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 在这个例子中，如果java后面跟了script则不会匹配java --&gt;</span><br><span class="line">var str &#x3D; &#39;javascript&#39;;</span><br><span class="line">var pattern &#x3D; &#x2F;java(?!script)&#x2F;;</span><br><span class="line">console.log(pattern.exec(str)); &#x2F;&#x2F; null</span><br></pre></td></tr></table></figure>

<h1 id="RegExp-的实例"><a href="#RegExp-的实例" class="headerlink" title="RegExp 的实例"></a>RegExp 的实例</h1><h2 id="方法-1"><a href="#方法-1" class="headerlink" title="方法"></a>方法</h2><table>
<thead>
<tr>
<th>方法</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>test(str)</td>
<td>返回布尔值。判断该字符串是否有该匹配式</td>
</tr>
<tr>
<td>exec(str)</td>
<td>返回数组。找到该匹配式，将匹配成功的字符放入数组中</td>
</tr>
<tr>
<td>toString()</td>
<td>返回字符串。将匹配式转换为字面量的字符串</td>
</tr>
<tr>
<td>toLocaleString()</td>
<td>返回本地字符。其实没多大区别</td>
</tr>
<tr>
<td>valueOf()</td>
<td>返回该匹配式的字符串形式</td>
</tr>
</tbody></table>
<h2 id="属性"><a href="#属性" class="headerlink" title="属性"></a>属性</h2><table>
<thead>
<tr>
<th>属性</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>ignoreCase</td>
<td>判断 flags 是否有i</td>
</tr>
<tr>
<td>global</td>
<td>判断 flags 是否有g</td>
</tr>
<tr>
<td>multiline</td>
<td>判断flags是否有m</td>
</tr>
<tr>
<td>source</td>
<td>返回字面量形式的字符串</td>
</tr>
<tr>
<td>lastIndex</td>
<td>返回数值。每次检索完毕的最终位置</td>
</tr>
</tbody></table>
<h2 id="构造函数属性"><a href="#构造函数属性" class="headerlink" title="构造函数属性"></a>构造函数属性</h2><table>
<thead>
<tr>
<th>构造函数属性</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>RegExp.input</td>
<td>返回检索的字符串</td>
</tr>
<tr>
<td>RegExp.$_</td>
<td>和 RegExp.input 作用相同</td>
</tr>
<tr>
<td>RegExp.lastMatch</td>
<td>返回最近一次匹配的字符</td>
</tr>
<tr>
<td>RegExp[‘$&amp;’]</td>
<td>和 RegExp.lastMatch 作用相同</td>
</tr>
<tr>
<td>RegExp.leftContext</td>
<td>上一次匹配左边剩余的字符</td>
</tr>
<tr>
<td>RegExp.rightContext</td>
<td>上一次匹配右边边剩余的字符</td>
</tr>
<tr>
<td>RegExp.lastParen</td>
<td>上一次捕获的分组的字符</td>
</tr>
<tr>
<td>RegExp.$1</td>
<td>第一个分组的引用，与<code>\1</code>作用相同</td>
</tr>
</tbody></table>
<h1 id="String对象中与正则表达式相关的方法"><a href="#String对象中与正则表达式相关的方法" class="headerlink" title="String对象中与正则表达式相关的方法"></a>String对象中与正则表达式相关的方法</h1><h2 id="search"><a href="#search" class="headerlink" title="search"></a>search</h2><p>返回一个 Number ，找到匹配的字符的位置</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var str &#x3D; &quot;html js&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;js&#x2F;;</span><br><span class="line">console.log(str.search(pattern)); &#x2F;&#x2F; 5</span><br></pre></td></tr></table></figure>

<h2 id="match"><a href="#match" class="headerlink" title="match"></a>match</h2><p>和exec一样，返回的是一个类数组的对象</p>
<h3 id="区别"><a href="#区别" class="headerlink" title="区别"></a>区别</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">var str &#x3D; &quot;js js js&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;js&#x2F;g;</span><br><span class="line">console.log(str.match(pattern)); &#x2F;&#x2F; [&quot;js&quot;, &quot;js&quot;, &quot;js&quot;]</span><br></pre></td></tr></table></figure>

<p><code>match</code>： 非全局的情况下才会返回分组中匹配到的内容，全局匹配只能匹配到所有匹配到的字符</p>
<p><code>exec</code>：无论是否全局匹配都会返回分组中匹配到的内容，都只会返回当前匹配到的一个内容，而不是全部返回</p>
<h2 id="replace"><a href="#replace" class="headerlink" title="replace"></a>replace</h2><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&lt;!-- 全部替换 --&gt;</span><br><span class="line">var str &#x3D; &quot;I love js js&quot;;</span><br><span class="line">var pattern &#x3D; &#x2F;js&#x2F;g;</span><br><span class="line">console.log(str.replace(pattern, &quot;html&quot;));</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/hexo/tags/JavaScript/" rel="tag"># JavaScript</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/hexo/2020/11/02/npm%E7%9B%B8%E5%85%B3%E5%91%BD%E4%BB%A4/" rel="prev" title="npm相关命令">
      <i class="fa fa-chevron-left"></i> npm相关命令
    </a></div>
      <div class="post-nav-item">
    <a href="/hexo/2020/12/07/git%E6%9B%B4%E6%96%B0fork%E4%BB%93%E5%BA%93%E4%BB%A3%E7%A0%81/" rel="next" title="git更新fork仓库代码">
      git更新fork仓库代码 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BB%80%E4%B9%88%E6%98%AF%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F"><span class="nav-number">1.</span> <span class="nav-text">什么是正则表达式</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%B9%E6%B3%95"><span class="nav-number">2.</span> <span class="nav-text">方法</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#test"><span class="nav-number">2.1.</span> <span class="nav-text">test</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#exec"><span class="nav-number">2.2.</span> <span class="nav-text">exec</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#flags"><span class="nav-number">3.</span> <span class="nav-text">flags</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6"><span class="nav-number"></span> <span class="nav-text">转义字符</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%87%8D%E5%A4%8D"><span class="nav-number"></span> <span class="nav-text">重复</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%9D%9E%E8%B4%AA%E5%A9%AA%E5%8C%B9%E9%85%8D"><span class="nav-number">1.</span> <span class="nav-text">非贪婪匹配</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%80%89%E6%8B%A9%E3%80%81%E5%88%86%E7%BB%84%E3%80%81%E5%BC%95%E7%94%A8"><span class="nav-number"></span> <span class="nav-text">选择、分组、引用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%80%89%E6%8B%A9"><span class="nav-number">1.</span> <span class="nav-text">选择</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%88%86%E7%BB%84"><span class="nav-number">2.</span> <span class="nav-text">分组</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%BC%95%E7%94%A8"><span class="nav-number">3.</span> <span class="nav-text">引用</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%A6%96%E5%B0%BE%E5%8C%B9%E9%85%8D"><span class="nav-number"></span> <span class="nav-text">首尾匹配</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%89%8D%E7%9E%BB%E6%80%A7%E5%8C%B9%E9%85%8D"><span class="nav-number"></span> <span class="nav-text">前瞻性匹配</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%B4%9F%E5%90%91%E5%89%8D%E7%9E%BB%E6%80%A7%E5%8C%B9%E9%85%8D"><span class="nav-number"></span> <span class="nav-text">负向前瞻性匹配</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#RegExp-%E7%9A%84%E5%AE%9E%E4%BE%8B"><span class="nav-number"></span> <span class="nav-text">RegExp 的实例</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%96%B9%E6%B3%95-1"><span class="nav-number">1.</span> <span class="nav-text">方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%B1%9E%E6%80%A7"><span class="nav-number">2.</span> <span class="nav-text">属性</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E5%B1%9E%E6%80%A7"><span class="nav-number">3.</span> <span class="nav-text">构造函数属性</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#String%E5%AF%B9%E8%B1%A1%E4%B8%AD%E4%B8%8E%E6%AD%A3%E5%88%99%E8%A1%A8%E8%BE%BE%E5%BC%8F%E7%9B%B8%E5%85%B3%E7%9A%84%E6%96%B9%E6%B3%95"><span class="nav-number"></span> <span class="nav-text">String对象中与正则表达式相关的方法</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#search"><span class="nav-number">1.</span> <span class="nav-text">search</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#match"><span class="nav-number">2.</span> <span class="nav-text">match</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%8C%BA%E5%88%AB"><span class="nav-number">2.1.</span> <span class="nav-text">区别</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#replace"><span class="nav-number">3.</span> <span class="nav-text">replace</span></a></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="幽辰"
      src="/hexo/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">幽辰</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/hexo/archives/">
        
          <span class="site-state-item-count">127</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/hexo/categories/">
          
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/hexo/tags/">
          
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



      </div>

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2021</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">幽辰</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a>
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/hexo/lib/anime.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.min.js"></script>
  <script src="/hexo/lib/velocity/velocity.ui.min.js"></script>

<script src="/hexo/js/utils.js"></script>

<script src="/hexo/js/motion.js"></script>


<script src="/hexo/js/schemes/pisces.js"></script>


<script src="/hexo/js/next-boot.js"></script>




  















  

  

</body>
</html>
